package com.github.live.base;

import android.database.sqlite.SQLiteException;

import com.github.livebase.bean.EpgInformation;
import com.github.livebase.database.BookingOperator;
import com.github.livebase.database.LiveDatabaseHelper;
import com.github.livebase.event.Event;
import com.github.livebase.event.EventBus;
import com.github.livebase.event.EventListener;
import com.github.livebase.event.SimpleEventBus;

import java.util.Date;

/**
 * @ClassName AppointmentManager
 * @description: 节目的预约管理，time以分钟为单位
 * @author: biao532
 * @create: 2024-01-04 16:19
 * @Version 1.0
 **/
public class BookingManager implements EventListener {
    private static final String booking_type = "program_booking";
    private BookingOperator operator;
    private static volatile BookingManager manager;

    public static BookingManager singleton() {
        if (manager == null) {
            synchronized (BookingManager.class) {
                if (manager == null) {
                    manager = new BookingManager(LiveApplication.getContext().getLiveDatabaseHelper());
                    SimpleEventBus.getDefault().register(EventBus.EVENT_CHANGE_DATE, manager, true);
                }
            }
        }
        return manager;
    }
    private BookingManager(LiveDatabaseHelper helper) {
        this.operator = helper.getBooking();
    }

    /**
     * 预约节目
     * @param information
     * @return
     */
    public boolean booking(EpgInformation information) {
        if (isBooking(information)) {
            return false;
        }
        long time = information.getStart() - information.getStart() % 60000;
        operator.booking(time, booking_type, store2String(information));
        return true;
    }

    /**
     * 节目是否已预约
     * @param information
     * @return
     */
    public boolean isBooking(EpgInformation information) {
        long time = information.getStart() - information.getStart() % 60000;
        return operator.exist(time, booking_type);
    }

    /**
     * 移除预约的 节目信息
     * @param information
     * @return
     */
    public boolean remove(EpgInformation information) {
        if (!isBooking(information)) {
            return true;
        }
        long time = information.getStart() - information.getStart() % 60000;
        try {
            operator.delete(time, booking_type);
            return true;
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查找 time 的预约的节目信息
     * @param time
     * @return
     */
    public EpgInformation select(long time) {
        time = time - time % 60000;
        BookingOperator.Booking b = operator.select(time, booking_type);
        if (b == null || b.getMessage() == null) {
            return null;
        }
        return read2Information(b.getMessage());
    }

    /**
     * 将EpgInformation转化称str
     * @param information
     * @return
     */
    private String store2String(EpgInformation information) {
        StringBuilder builder = new StringBuilder();
        builder.append(information.getStart());
        builder.append('\2');
        builder.append(information.getEnd());
        builder.append('\2');
        builder.append(information.getChannelId());
        builder.append('\2');
        builder.append(information.getTitle());
        return builder.toString();
    }

    /**
     * 将str转化成EpgInformation
     * @param str
     * @return
     */
    private EpgInformation read2Information(String str) {
        String[] datas = str.split("\2");
        EpgInformation information = new EpgInformation(datas[2], Long.valueOf(datas[0]), Long.valueOf(datas[1]));
        information.setTitle(datas[3]);
        return information;
    }

    @Override
    public void onEvent(Event event) {
        if (!Event.isEvent(event, EventBus.EVENT_CHANGE_DATE)) {
            return;
        }
        // 监听日期变化，移除当前时间前的所有预约
        Date date = event.getAttachObject();
        operator.deleteBefore(date.getTime(), booking_type);
    }
}
